<?php
declare(strict_types = 1);
namespace app\admin\business;

use think\facade\Log;
use think\facade\Session;
use app\common\model\mysql\AdminUser as AdminUserModel;

class AdminUser
{
    public $adminUserModel = null;

    public function __construct()
    {
        $this->adminUserModel = new AdminUserModel();
    }

    public function login(array $data)
    {
        $adminUser = $this->adminUserModel->findAdminUserByUsername($data['username']);

        if (!$adminUser) {
//            return show(config('status.error'), '没有该用户');
            throw new \think\Exception("没有该用户");
        }

        if ($adminUser['password'] != pwdEncryption($data['password'])) {
//            return show(config('status.error'), '密码不正确');
            throw new \think\Exception("密码不正确");

        }

        $updateData = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
        ];

        $res = $this->adminUserModel->updateAdminUserById($adminUser['id'], $updateData);
        if (!$res) {
//            return show(config('status.error'), '更新用户数据失败');
            throw new \think\Exception("更新用户数据失败");

        }

        Session::set(config('admin.session_admin'), $adminUser);

        return true;
    }



}
